package com.cdm.beans.user;

import java.util.List;

import net.sf.frails.JSFHelper;
import net.sf.frails.aop.annotations.InjectBean;
import net.sf.frails.bean.annotations.DefMbean;
import net.sf.frails.bean.annotations.InVar;
import net.sf.frails.bean.annotations.PreRender;
import net.sf.frails.bean.annotations.Prop;
import net.sf.frails.bean.annotations.ScopeType;

import com.cdm.domain.dao.UserDAO;
import com.cdm.domain.service.Service;
import com.cdm.domain.service.ServiceImpl;
import com.cdm.persistence.User;
import com.cdm.util.Constants;
import com.vivo.util.DTool;

@DefMbean
public class AdminBean {

	private static final String thisPage = "/user/admin";

	@Prop
	User loginUser;

	@InVar
	@Prop
	User user;

	@Prop
	List<User> unauths;
	@Prop
	List<User> admins;
	@Prop
	List<User> cps;
	@Prop
	List<User> sps;
	@Prop
	List<User> cdms;
	@Prop
	List<User> endUsers;

	@Prop
	boolean create = true;

	@PreRender(failOutcome = thisPage)
	public boolean init() {
		Object object = JSFHelper.getFromApplication(Constants.LOGIN_USER_KEY);
		if (object instanceof User && object != null) {
			loginUser = (User) object;
		}
		if (create == true) {
			this.unauths = userDAO.getByProperty(UserDAO.AUTH_STATUS, User.UNAUTH);
			this.admins = service.getAuthedUsersByRole(User.ROLE_ADMIN);
			this.cdms = service.getAuthedUsersByRole(User.ROLE_CDM);
			this.cps = service.getAuthedUsersByRole(User.ROLE_CP);
			this.sps = service.getAuthedUsersByRole(User.ROLE_SP);
			this.endUsers = service.getAuthedUsersByRole(User.ROLE_END_USER);
			// create = false;
		}

		DTool.d.debug("Enter Admins Home Page");
		return true;
	}

	@InjectBean
	public String auth() {
		service.auth(user);
		create = true;
		return thisPage;
	}

	@InjectBean
	public String delete() {
		service.delete(user);
		create = true;
		return thisPage;
	}

	@InjectBean
	public String detail() {
//		service.delete(user);
		create = true;
		return thisPage;
	}

	private UserDAO userDAO = new UserDAO();
	private Service service = new ServiceImpl();
}
